#!/bin/bash 

# Date 		: 2021.02.27
# Author	: majin
# Mail 		: xx
# Function	: test for mysql
# Version 	: 1.0
# Update 	: 2021.02.27
# sh
# base
# /home/majin/install/mysql-8.0.17/build/bin/mysqld --defaults-file=/home/majin/install/mysql-8.0.17/my.cnf
# /home/majin/install/mysql-8.0.17/build/bin/mysqladmin -uroot -p123456 shutdown -S /home/majin/test/data/mysql/run/mysql.sock
# hfsort
# /home/majin/install/mysql-8.0.17/build-hfsort/bin/mysqld --defaults-file=/home/majin/install/mysql-8.0.17/my-hfsort.cnf 
# /home/majin/install/mysql-8.0.17/build-hfsort/bin/mysqladmin -uroot -p123456 shutdown -S /home/majin/test/data-hfsort/mysql/run/mysql.sock
# /home/lhl/workspace/mysql/client/benchmarksql5.0-for-mysql/run/runBenchmark.sh /home/lhl/workspace/mysql/client/benchmarksql5.0-for-mysql/run/majin_my_mysql.properties
# 1037146
mysql_home="/home/majin/install/mysql-8.0.17"

benchmark_dir="/home/lhl/workspace/mysql/client/benchmarksql5.0-for-mysql/run"

test_dir="/home/majin/test"
result_dir="${test_dir}/log"
data_dir="${test_dir}/data"
cfg_dir="${test_dir}/cfg"

now_dir=`pwd`
cd ${benchmark_dir}
test=30
for ((i=0;i<${test};i++))
do
    for program in "" "-hfsort-x86" # "" "-hfsort"
    do
        echo "test for $i, program: mysqld${program}"
        
        mysqld_dir="${mysql_home}/build${program}/bin"

        log_file="${result_dir}/result${program}.log"
        tmp_log="${test_dir}/tmp.log"

        sock_file="${data_dir}/data${program}/mysql/run/mysql.sock"
        cnf_file="${cfg_dir}/my${program}.cnf"
        properties="${cfg_dir}/majin_my_mysql${program}.properties"


        if [ $i == "0" ];then
            now_time=`date`
            if [ -f ${log_file} ];then
                # mv ${log_file} ${log_file}.old 
                echo "[majin] clean log file: ${log_file}"
            fi
            echo "[majin] create log file: ${log_file} , ${tmp_log}" 
            echo "test start at $now_time " > ${log_file} 2>&1
            echo "test start at $now_time " > ${tmp_log} 2>&1
        fi

        echo "[majin] log_file: ${log_file}" 2>&1 | tee -a ${tmp_log}
        echo "[majin] mysqld_dir: ${mysqld_dir}" 2>&1 | tee -a ${tmp_log}
   
        # numactl -C '0-21,24-45,48-69,72-93' 
        echo "[majin] $i test begin" 2>&1 | tee -a ${tmp_log}

        echo "[majin] ${mysqld_dir}/mysqld run" 2>&1 | tee -a ${tmp_log}
        taskset -c 10 ${mysqld_dir}/mysqld --defaults-file=${cnf_file} 2>&1 | tee -a ${tmp_log} &
        mysqld_pid=$(jobs -p)
        echo "[majin] mysqld_pid: ${mysqld_pid}" 2>&1 | tee -a ${tmp_log}
        sleep 30

        echo "[majin] runDatabaseDestroy " 2>&1 | tee -a ${tmp_log}
        ./runDatabaseDestroy.sh ${properties} >> ${tmp_log} 2>&1
        sleep 10

        echo "[majin] build_ttcp " 2>&1 | tee -a ${tmp_log}
        ${mysqld_dir}/mysql -uroot -p123456 -S ${sock_file}  -e "show databases;create database tpcc;show databases;use tpcc;show tables;" >> ${tmp_log} 2>&1

        echo "[majin] runDatabaseBuild " 2>&1 | tee -a ${tmp_log}
        ./runDatabaseBuild.sh ${properties} >> ${tmp_log} 2>&1
        sleep 10

        echo "[majin] runBenchmark " 2>&1 | tee -a ${tmp_log}
        # numactl -C '95-127' 
        taskset -c 20 ./runBenchmark.sh ${properties} >> ${log_file} 2>&1
        sleep 10
        
        echo "[majin] runDatabaseDestroy " 2>&1 | tee -a ${tmp_log}
        ./runDatabaseDestroy.sh ${properties} >> ${tmp_log} 2>&1
        sleep 10

        echo "[majin] mysqladmin  shutdown " 2>&1 | tee -a ${tmp_log}
        ${mysqld_dir}/mysqladmin -uroot -p123456 shutdown -S ${sock_file} 2>&1 | tee -a ${tmp_log}
        sleep 10

        echo "[majin] $i test end" 2>&1 | tee -a ${tmp_log}
    done 
done
cd ${now_dir}


